

// 实现自己的debounce
function debounce(fn, delay, immediate = false, callBack = function () { }) {
    let timer = null
    let isNow = false
    const _decounce = function () {
        if (immediate && !isNow) {
            callBack(fn.apply(this, [...arguments]))

            isNow = true
            return
        }
        if (timer) {
            clearTimeout(timer)
            timer = null
        }

        timer = setTimeout(() => {
            callBack(fn.apply(this, [...arguments]))
            isNow = false
            timer = null
        }, delay);
    }

    _decounce.cancel = function () {
        if (timer) clearTimeout(timer)
        timer = null
        isNow = false
    }

    return _decounce
}

